<?php

namespace app\common\library;

//use app\common\model\UserMsg;

use think\db;

/**
 * @title YuActivity
 * @description 用户活动，赠送积分、发送消息等操作
 * @package app\common\library
 */
class YuActivity
{
    const VERSION = '6.0.5';

    protected $errorString = '';

    public static $instance ;

    /**
     * @title 单例调用
     * @description 单例调用
     * @author Admin ldy
     * @time 2018/9/25  12:22
     */
    public static function getInstance(){
        if (YuActivity::$instance == null){
            YuActivity::$instance = new \app\common\library\YuActivity;
            return YuActivity::$instance;
        }else{
            return YuActivity::$instance;
        }
    }

    public function __construct(){
        //初始化
    }

    public function setErrorString($msg){
        $this->errorString = $msg;
    }

    public function getErrorString(){
        return $this->errorString;
    }

    public static function addMessageQBase($jobHandlerClassName,$jobData,$level='2'){
        $isPushed = \app\common\library\YuUtil::postMsgByLevel($jobHandlerClassName, $jobData,$level);
        if ($isPushed) {
            $pushTime = date('Y-m-d H:i:s') ."  ".$jobHandlerClassName."  Job is Pushed to the MQ" . "<br>";
            return new ReSuccess('任务加入队列 成功！',$pushTime);
        } else {
            $pushTime = 'Oops, something went wrong.';
            return new ReFail('任务加入队列 失败！',$pushTime);
        }
    }

    //注册活动
    public static function registerActivity($email){
//        $jobHandlerClassName = 'app\common\job\AddPersonUrl';
//        $jobData = ['token' => $email];
//        return YuActivity::addMessageQBase($jobHandlerClassName,$jobData);
    }

    //在后台发送消息给用户，非阻塞
    public static function sendInnerMsgToUserInBackGround($uid,$title,$content){
        $jobHandlerClassName = 'app\common\job\AddPersonMessage';
        $jobData = ['uid' => $uid,'title'=>$title,'content'=>$content];
        return YuActivity::addMessageQBase($jobHandlerClassName,$jobData);
    }

    //在后台发送消息给用户，非阻塞（通过email）
    public static function sendInnerMsgToUserInBackGroundByEmail($email,$title,$content){
        $jobHandlerClassName = 'app\common\job\AddPersonMessage';
        $jobData = ['email'=>$email,'uid' => '','title'=>$title,'content'=>$content];
        return YuActivity::addMessageQBase($jobHandlerClassName,$jobData);
    }

    //在后台发送邮件给用户，非阻塞
    public static function sendEmailToUserInBackGround($email,$title,$content){
        $jobHandlerClassName = 'app\common\job\SendEmailToUser';
        $jobData = ['email' => $email,'title'=>$title,'content'=>$content];
        return YuActivity::addMessageQBase($jobHandlerClassName,$jobData);
    }

    //在后台发送邮件给用户，非阻塞(根据用户id)，level为优先级，普通为2，优先为1
    public static function sendEmailToUserInBackGroundByUserId($uid,$title,$content,$level='2'){
        $jobHandlerClassName = 'app\common\job\SendEmailToUserByUserId';
        $jobData = ['uid' => $uid,'title'=>$title,'content'=>$content];
        return YuActivity::addMessageQBase($jobHandlerClassName,$jobData,$level);
    }

    //在后台发放积分给用户，非阻塞
    public static function sendScoreToUserInBackGround($uid,$score){
        $jobHandlerClassName = 'app\common\job\AddPersonScore';
        $jobData = ['uid' => $uid,'score'=>$score];
        return YuActivity::addMessageQBase($jobHandlerClassName,$jobData);
    }

    //在后台发放经验值给用户，非阻塞
    public static function sendExperienceToUserInBackGround($uid,$experience){
        $jobHandlerClassName = 'app\common\job\AddPersonExperience';
        $jobData = ['uid' => $uid,'experience'=>$experience];
        return YuActivity::addMessageQBase($jobHandlerClassName,$jobData);
    }

    //在后台给用户规定不能删除的类型，非阻塞
    public static function addUnDeleteTypeRuleForUserInBackGround($uid,$data){
        $jobHandlerClassName = 'app\common\job\AddPersonRuleUnDelteType';
        $jobData = ['uid' => $uid,'data'=>$data];
        return YuActivity::addMessageQBase($jobHandlerClassName,$jobData);
    }

    public static function autoCalcNum($option=array()){
        return rand(10,99);
    }

    //给用户一个随机的奖励,title:更新【'.$key.'】成功！
    public static function rewardNormal($uid,$title){
        $score = YuActivity::autoCalcNum();
        $exp = YuActivity::autoCalcNum();
        //时间
        $time = date('Y-m-d H:i:s');
        //发送经验值
        $re1=sendExpTo($uid,$score);
        //发送积分奖励
        $re2=sendScoreTo($uid,$exp);
        //发送站内信
        $content = '恭喜您！'.$time.$title.' 获得['.$score.']积分、获得['.$exp.']经验值。请注意查收您的收获哦^_^ ';
        $re3=sendMsgToUser($uid,$title,$content);
        if ($re1->getStatus() && $re2->getStatus() && $re3->getStatus()){
            return new ReSuccess('rewardNormal成功！');
        }else{
            return new ReFail(ErrorType::ACTION_FAILED);
        }
    }
    //处理上传的的用户数据
    public static function uploadUserData($uid,$jsonstring){
        $message = '后台正在处理上传的数据……';
        //推送消息给前端，前端自动刷新
        \app\common\library\YuPust::pushMsgToUid($uid,$message);
        $jobHandlerClassName = 'app\common\job\UploadPersonData';
        $jobData = ['uid' => $uid,'data'=>$jsonstring];
        return YuActivity::addMessageQBase($jobHandlerClassName,$jobData,'1');
    }


}
